class ArticlesController < ApplicationController
	include ArticlesHelper

	before_filter :require_login, only: [:new, :create, :edit, :update, :destroy]

	def index
		@articles = Article.paginate(:page => params[:page], :per_page => 5)
	end

	def show
		@article = Article.find(params[:id])
		@commentable = @article
		@comments = @commentable.comments
		@comment = Comment.new
	end

	def new
		@article = Article.new
	end

	def create
		binding.pry
		@article = Article.new(article_params)
		if @article.save
		  redirect_to article_path(@article)
		else
		  render 'new'
		end
	end

	def destroy
		@article = Article.find(params[:id])
		@article.destroy
		redirect_to articles_path
	end

	def edit
		@article = Article.find(params[:id])
	end

	def update
		@article = Article.find(params[:id])
		@article.update(article_params)

		flash.notice = "Article '#{@article.title}' updated"

		redirect_to article_path(@article)
	end

	def preview
		@content = params[:content]
		respond_to do |format|
		  format.js
		end
	end
end
